package com.geccocrawler.gecco.demo.shijiazhuang;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.JsonPipeline;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: weixiaohu
 * Date: 2019/6/3
 * Time: 15:05
 */
@PipelineName("sjzPipeline")
public class ProcessPipeline implements Pipeline<Results> {
    private List allData = Lists.newCopyOnWriteArrayList();
    @Override
    public void process(Results jo) {
        HttpRequest currRequest = jo.getRequest();
        String url = currRequest.getUrl();
        int total = jo.getTotal();
        int size = jo.getSize();
        int totalPage = total/size + 1;
        int currPage = jo.getPage();
        String type = jo.getType();
        int nextPage = currPage + 1;
        long currentTime = jo.getCurrentTime();
        allData.addAll(jo.getList());
        if(nextPage <= totalPage) {
            String nextUrl = StringUtils.replaceOnce(url, "n=" + currPage, "n=" + nextPage);
            nextUrl = StringUtils.replaceOnce(nextUrl,"_="+ currentTime,"_=" + System.currentTimeMillis());
            DeriveSchedulerContext.into(currRequest.subRequest(nextUrl));
        }else{
            genExcel(allData,type);
        }

    }

    private void genExcel(List<Item> rows, String type){
        ExcelWriter writer = ExcelUtil.getWriter("d:/spider/"+type+".xlsx");
        writer.addHeaderAlias("name", "名称");
        writer.addHeaderAlias("code", "编码");
        writer.addHeaderAlias("address", "地址");
        writer.addHeaderAlias("level", "级别名称");
        writer.addHeaderAlias("ownerRegion", "所属经办机构");
        writer.write(rows, true);
        writer.close();
    }
}
